//
//  MoviesDateListVC.m
//  MovieDate
//
//  Created by ann on 16/4/8.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "MoviesDateListVC.h"
#import "Movie.h"
#import "UIImageView+WebCache.h"
#import "VariableHeader.h"
#import "MovieDetailVC.h"
@interface MoviesDateListVC ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger index;

}
@property(nonatomic,strong) NSMutableArray *movies;

@end

@implementation MoviesDateListVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [self initView];
     [self initData];
    
    // Do any additional setup after loading the view.
}

-(NSMutableArray *)movies{
    if (!_movies) {
        _movies = [NSMutableArray array];
    }
    return _movies;
}

-(void)initView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.pTableView.delegate = self;
    self.pTableView.dataSource = self;

    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         index = 1;
        [self refreshHeaderAction:index];
    }];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        index++;
        [self refreshFooterAction:index];
    }];
}

-(void)initData{

    if(!_movies){
        __weak typeof(self) weakSelf = self;
        [SVProgressHUD show];
        [TDNetwork requestMoviesPOST:movieListIP andPage:1 andPageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
            if (succeed) {
                [SVProgressHUD dismiss];
                NSDictionary *resDic =[dictionary valueForKey:@"res"];
                NSArray *movieList = [resDic valueForKey:@"filmList"];
                for (NSDictionary *movieDic in movieList) {
                    Movie *m = [[Movie alloc]init];
                    m.moiveImage = [movieDic valueForKey:@"moiveImage"];
                    m.moiveTitle = [movieDic valueForKey:@"moiveTitle"];
                    m.releaseTime =[[[NSString stringWithFormat:@"%@",[movieDic valueForKey:@"releaseTime"]] componentsSeparatedByString:@"/"] firstObject];
                 
                    m.appNum = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"appNum"]];
                    m.moiveScore =  [NSString stringWithFormat:@"%@",  [movieDic valueForKey:@"moiveScore"]];
                    m.appId =  [NSString stringWithFormat:@"%@", [movieDic valueForKey:@"appId"]];
                    
                    m.introduction = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"introduction"]];
                    m.appointmentCount = [NSString stringWithFormat:@"%@个影约",[movieDic valueForKey:@"appointmentCount"]];
                    m.moiveTime = [[[NSString stringWithFormat:@"%@",[movieDic valueForKey:@"moiveTime"]] componentsSeparatedByString:@"("] firstObject];
                    m.moiveType = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"moiveType"]];
                    
                    m.movieId = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"movieId"]];
                    m.director = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"director"]];
                    m.countries = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"countries"]];
                    m.placeOfIssue = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"placeOfIssue"]];
                    
                    [weakSelf.movies addObject:m];
                }
                [weakSelf.pTableView reloadData];
                index = 1;
            }else{
                [SVProgressHUD showErrorWithStatus:@"请求错误"];
                [SVProgressHUD dismiss];
                
            }
        }];
        
    }
}

-(void)refreshFooterAction:(NSInteger)index{
            __weak typeof(self) weakSelf = self;
    
    [TDNetwork requestMoviesPOST:movieListIP andPage:index andPageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            NSDictionary *resDic =[dictionary valueForKey:@"res"];
            NSArray *movieList = [resDic valueForKey:@"filmList"];
            for (NSDictionary *movieDic in movieList) {
                Movie *m = [[Movie alloc]init];
                m.moiveImage = [movieDic valueForKey:@"moiveImage"];
                m.moiveTitle = [movieDic valueForKey:@"moiveTitle"];
              m.releaseTime =[[[NSString stringWithFormat:@"%@",[movieDic valueForKey:@"releaseTime"]] componentsSeparatedByString:@"/"] firstObject];
                m.appNum = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"appNum"]];
                m.moiveScore =  [NSString stringWithFormat:@"%@",  [movieDic valueForKey:@"moiveScore"]];
                m.appId =  [NSString stringWithFormat:@"%@", [movieDic valueForKey:@"appId"]];
                
                m.introduction = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"introduction"]];
               m.appointmentCount = [NSString stringWithFormat:@"%@个影约",[movieDic valueForKey:@"appointmentCount"]];
                      m.moiveTime = [[[NSString stringWithFormat:@"%@",[movieDic valueForKey:@"moiveTime"]] componentsSeparatedByString:@"("] firstObject];
                m.moiveType = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"moiveType"]];
                
                m.movieId = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"movieId"]];
                m.director = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"director"]];
                m.countries = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"countries"]];
                m.placeOfIssue = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"placeOfIssue"]];
                
                [weakSelf.movies addObject:m];
            }
            [weakSelf.pTableView reloadData];
            [self.pTableView.mj_footer endRefreshing];
            [self.pTableView.mj_header endRefreshing];
        }else{
     
            
        }
    }];



}
-(void)refreshHeaderAction:(NSInteger)index{
    __weak typeof(self) weakSelf = self;
    
    [TDNetwork requestMoviesPOST:movieListIP andPage:index andPageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            [self.movies removeAllObjects];
            NSDictionary *resDic =[dictionary valueForKey:@"res"];
            NSArray *movieList = [resDic valueForKey:@"filmList"];
            for (NSDictionary *movieDic in movieList) {
                Movie *m = [[Movie alloc]init];
                m.moiveImage = [movieDic valueForKey:@"moiveImage"];
                m.moiveTitle = [movieDic valueForKey:@"moiveTitle"];
                m.releaseTime =[[[NSString stringWithFormat:@"%@",[movieDic valueForKey:@"releaseTime"]] componentsSeparatedByString:@"/"] firstObject];
                m.appNum = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"appNum"]];
                m.moiveScore =  [NSString stringWithFormat:@"%@",  [movieDic valueForKey:@"moiveScore"]];
                m.appId =  [NSString stringWithFormat:@"%@", [movieDic valueForKey:@"appId"]];
                
                m.introduction = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"introduction"]];
                m.appointmentCount = [NSString stringWithFormat:@"%@个影约",[movieDic valueForKey:@"appointmentCount"]];
                  m.moiveTime = [[[NSString stringWithFormat:@"%@",[movieDic valueForKey:@"moiveTime"]] componentsSeparatedByString:@"("] firstObject];
                m.moiveType = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"moiveType"]];
                
                m.movieId = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"movieId"]];
                m.director = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"director"]];
                m.countries = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"countries"]];
                m.placeOfIssue = [NSString stringWithFormat:@"%@",[movieDic valueForKey:@"placeOfIssue"]];
                
                [weakSelf.movies addObject:m];
            }
            [weakSelf.pTableView reloadData];
            [self.pTableView.mj_footer endRefreshing];
            [self.pTableView.mj_header endRefreshing];
        }else{
  
            
        }
    }];
    
    
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.movies.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idMoviesDateListVC"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"idMoviesDateListVC"];
    }
    
    Movie *m = [self.movies objectAtIndex:indexPath.row];
    [self setCellMessageWithCell:cell andMovie:m];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView  deselectRowAtIndexPath:indexPath animated:NO];
        Movie *m =  [self.movies objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"idfMovieDetailVC" sender:m];

}
-(void)setCellMessageWithCell:(UITableViewCell *)cell andMovie:(Movie *)movie{
     UIImageView *moviePic =  [cell viewWithTag:1050];
     UILabel *movieTitle =  [cell viewWithTag:1051];
     UILabel *movieTime =  [cell viewWithTag:1052];
     UILabel *movieType =  [cell viewWithTag:1053];
     UILabel *countries =  [cell viewWithTag:1054];
     UILabel *director =  [cell viewWithTag:1055];
     UILabel *releaseTime = [cell viewWithTag:1056];
    UILabel *appNum = [cell viewWithTag:1057];
    UILabel *introduction = [cell viewWithTag:1058];
    
    [moviePic sd_setImageWithURL:[NSURL URLWithString:movie.moiveImage]];
    if ([movie.movieId isEqualToString:@"105"]) {
            countries.text = @"美国";
        movieTitle.text = movie.moiveTitle;
        movieTime.text = movie.moiveTime;
        movieType.text = movie.moiveType;
        director.text = movie.director;
        releaseTime.text = movie.releaseTime;
        appNum.text = [NSString stringWithFormat:@"%@个影约",movie.appNum];
        introduction.text = movie.introduction;
    }else{
    
        movieTitle.text = movie.moiveTitle;
        movieTime.text = movie.moiveTime;
        movieType.text = movie.moiveType;
        countries.text = movie.countries;
        director.text = movie.director;
        releaseTime.text = movie.releaseTime;
        appNum.text = [NSString stringWithFormat:@"%@个影约",movie.appNum];
        introduction.text = movie.introduction;

    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"idfMovieDetailVC"]) {
        MovieDetailVC *movieDetailVC = segue.destinationViewController;
        movieDetailVC.movie = (Movie *)sender;
    }

}


@end
